package bawei.com.bilibili;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Toast;

import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import bawei.com.adapter.SopCastAdapter;
import bawei.com.model.BaseCallback;

import bawei.com.model.Network;
import bawei.com.model.OkHttpHelper;
import bawei.com.model.bean.SopCastBean;

/**
 * 作者： 南山君
 * 时间：2017/3/13.15:24
 */

public class ZoneActivity extends Activity {

    private RecyclerView recyclerView;

    private SopCastAdapter sopCastAdapter;

    private SwipeRefreshLayout swipeRefreshLayout;

    private Toolbar toolbar;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zone);

        toolbar= (Toolbar) findViewById(R.id.tool_bar_zone);
        tabLayout= (TabLayout) findViewById(R.id.tab_layout_zone);

        toolbar.setNavigationIcon(R.mipmap.ic_drawer_home);
        toolbar.setTitle("未登录");//设置主标题
        toolbar.inflateMenu(R.menu.nav_menu);//设置右上角的填充菜单
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int menuItemId = item.getItemId();
                if (menuItemId == R.id.a) {
                    Toast.makeText(ZoneActivity.this ,"菜单一" , Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.b) {
                    Toast.makeText(ZoneActivity.this , "菜单二" , Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.action_item1) {
                    Toast.makeText(ZoneActivity.this , "菜单三" , Toast.LENGTH_SHORT).show();

                } else if (menuItemId == R.id.action_item2) {
                    Toast.makeText(ZoneActivity.this , "菜单四" , Toast.LENGTH_SHORT).show();

                }
                return true;
            }
        });

        recyclerView = (RecyclerView) findViewById(R.id.recycle_zone);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout_zone);

        getRefresh();
        getbanner();

    }

    public void  getRefresh(){

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000); // 2秒后发送消息，停止刷新
            }
        });
        // 设置下拉圆圈上的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.pink_text_color);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setDistanceToTriggerSync(400);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }

    //请求轮播图数据
    private void getbanner() {

        OkHttpHelper.getinstance().get("http://live.bilibili.com/" + Network.HOME_DIRECT, new BaseCallback<SopCastBean>() {
            @Override
            public void onRequestBefore() {
            }
            @Override
            public void onFailure(Request request, Exception e) {
            }
            @Override
            public void onSuccess(Response response, SopCastBean sopCastBean) {

                sopCastAdapter = new SopCastAdapter(ZoneActivity.this,sopCastBean.getData());
                recyclerView.setAdapter(sopCastAdapter);
                GridLayoutManager layout = new GridLayoutManager(ZoneActivity.this, 12);
                layout.setOrientation(LinearLayoutManager.VERTICAL);
                layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                    @Override
                    public int getSpanSize(int position) {

                        return sopCastAdapter.getSpanSize(position);
                    }
                });

                recyclerView.setLayoutManager(layout);

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
            }
        });


    }



}
